using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sandbox.Definitions;
using Sandbox.Engine.Utils;
using Sandbox.Game.Entities;
using Sandbox.Game.Entities.Cube;
using Sandbox.Game.Gui;
using Sandbox.Graphics.GUI;
using VRage;
using VRage.Game;
using VRage.Game.Models;
using VRageMath;

namespace Sandbox.Game.Screens.DebugScreens
{
	[MyDebugScreen("Game", "Cube Blocks")]
	internal class MyGuiScreenDebugCubeBlocks : MyGuiScreenDebugBase
	{
		public static MySymmetryAxisEnum? DebugXMirroringAxis;

		public static MySymmetryAxisEnum? DebugYMirroringAxis;

		public static MySymmetryAxisEnum? DebugZMirroringAxis;

		private MyGuiControlSlider m_dummyDrawDistanceSlider;

		private MyGuiControlCombobox m_xMirroringCombo;

		private MyGuiControlCombobox m_yMirroringCombo;

		private MyGuiControlCombobox m_zMirroringCombo;

		private MyGuiControlLabel m_currentMirrorAxisLabel;

		private MyGuiControlLabel m_currentAxisLabel;

		public override string GetFriendlyName()
		{
			return "MyGuiScreenDebugCubeBlocks";
		}

		public MyGuiScreenDebugCubeBlocks()
		{
			RecreateControls(constructor: true);
		}

		public override void RecreateControls(bool constructor)
		{
			base.RecreateControls(constructor);
			m_scale = 0.7f;
			AddCaption("Cube blocks", Color.Yellow.ToVector4());
			AddShareFocusHint();
			m_currentPosition = -m_size.Value / 2f + new Vector2(0.02f, 0.1f);
			m_currentMirrorAxisLabel = AddLabel("Mirror axis: " + MyCubeBuilderGizmo.CurrentBlockMirrorAxis, Color.Yellow.ToVector4(), 1.2f);
			m_currentAxisLabel = AddLabel("Block axis: " + MyCubeBuilderGizmo.CurrentBlockMirrorOption, Color.Yellow.ToVector4(), 1.2f);
			m_currentPosition += new Vector2(0f, 0.015f);
			AddLabel("X symmetry", Color.Yellow.ToVector4(), 1.2f);
			m_xMirroringCombo = AddCombo();
			AddMirroringTypes(m_xMirroringCombo);
			m_xMirroringCombo.ItemSelected += m_xMirroringCombo_ItemSelected;
			AddLabel("Y symmetry", Color.Yellow.ToVector4(), 1.2f);
			m_yMirroringCombo = AddCombo();
			AddMirroringTypes(m_yMirroringCombo);
			m_yMirroringCombo.ItemSelected += m_yMirroringCombo_ItemSelected;
			AddLabel("Z symmetry", Color.Yellow.ToVector4(), 1.2f);
			m_zMirroringCombo = AddCombo();
			AddMirroringTypes(m_zMirroringCombo);
			m_zMirroringCombo.ItemSelected += m_zMirroringCombo_ItemSelected;
			AddButton(new StringBuilder("Symmetry reset"), onClick_SymmetryReset);
			m_dummyDrawDistanceSlider = AddSlider("Dummies draw distance", MyDebugDrawSettings.DEBUG_DRAW_MODEL_DUMMIES_DISTANCE, 0f, 100f);
			m_dummyDrawDistanceSlider.ValueChanged = DummyDrawDistanceSliderChanged;
			m_currentPosition += new Vector2(0f, 0.15f);
			AddCheckBox("Debug draw all mount points", MyDebugDrawSettings.DEBUG_DRAW_MOUNT_POINTS_ALL, onClick_DebugDrawMountPointsAll);
			AddCheckBox("Debug draw mount points", MyDebugDrawSettings.DEBUG_DRAW_MOUNT_POINTS, onClick_DebugDrawMountPoints);
			AddCheckBox("Forward", null, MemberHelper.GetMember(() => MyDebugDrawSettings.DEBUG_DRAW_MOUNT_POINTS_AXIS0));
			AddCheckBox("Backward", null, MemberHelper.GetMember(() => MyDebugDrawSettings.DEBUG_DRAW_MOUNT_POINTS_AXIS1));
			AddCheckBox("Left", null, MemberHelper.GetMember(() => MyDebugDrawSettings.DEBUG_DRAW_MOUNT_POINTS_AXIS2));
			AddCheckBox("Right", null, MemberHelper.GetMember(() => MyDebugDrawSettings.DEBUG_DRAW_MOUNT_POINTS_AXIS3));
			AddCheckBox("Up", null, MemberHelper.GetMember(() => MyDebugDrawSettings.DEBUG_DRAW_MOUNT_POINTS_AXIS4));
			AddCheckBox("Down", null, MemberHelper.GetMember(() => MyDebugDrawSettings.DEBUG_DRAW_MOUNT_POINTS_AXIS5));
			AddCheckBox("Draw autogenerated", null, MemberHelper.GetMember(() => MyDebugDrawSettings.DEBUG_DRAW_MOUNT_POINTS_AUTOGENERATE));
			AddCheckBox("CubeBlock Integrity", null, MemberHelper.GetMember(() => MyDebugDrawSettings.DEBUG_DRAW_BLOCK_INTEGRITY));
			AddCheckBox("Armor Block Tile Normals", null, MemberHelper.GetMember(() => MyDebugDrawSettings.DEBUG_DRAW_ARMOR_BLOCK_TILE_NORMALS));
			AddButton(new StringBuilder("Save generated mountpoints"), onClick_Save).VisualStyle = MyGuiControlButtonStyleEnum.Default;
			AddButton(new StringBuilder("Reload definitions"), onClick_Reload).VisualStyle = MyGuiControlButtonStyleEnum.Default;
		}

		private void m_xMirroringCombo_ItemSelected()
		{
			if (m_xMirroringCombo.GetSelectedIndex() != -1)
			{
				DebugXMirroringAxis = (MySymmetryAxisEnum)m_xMirroringCombo.GetSelectedKey();
			}
			else
			{
				DebugXMirroringAxis = null;
			}
		}

		private void m_yMirroringCombo_ItemSelected()
		{
			if (m_yMirroringCombo.GetSelectedIndex() != -1)
			{
				DebugYMirroringAxis = (MySymmetryAxisEnum)m_yMirroringCombo.GetSelectedKey();
			}
			else
			{
				DebugYMirroringAxis = null;
			}
		}

		private void m_zMirroringCombo_ItemSelected()
		{
			if (m_zMirroringCombo.GetSelectedIndex() != -1)
			{
				DebugZMirroringAxis = (MySymmetryAxisEnum)m_zMirroringCombo.GetSelectedKey();
			}
			else
			{
				DebugZMirroringAxis = null;
			}
		}

		private void AddMirroringTypes(MyGuiControlCombobox combo)
		{
			combo.Clear();
			foreach (object value in Enum.GetValues(typeof(MySymmetryAxisEnum)))
			{
				combo.AddItem((int)value, Enum.GetName(typeof(MySymmetryAxisEnum), value));
			}
		}

		private void DummyDrawDistanceSliderChanged(MyGuiControlSlider slider)
		{
			MyDebugDrawSettings.DEBUG_DRAW_MODEL_DUMMIES_DISTANCE = slider.Value;
		}

		private void onClick_DebugDrawMountPointsAll(MyGuiControlCheckbox obj)
		{
			MyDebugDrawSettings.DEBUG_DRAW_MOUNT_POINTS_ALL = obj.IsChecked;
		}

		private void onClick_DebugDrawMountPoints(MyGuiControlCheckbox sender)
		{
			MyDebugDrawSettings.DEBUG_DRAW_MOUNT_POINTS = sender.IsChecked;
		}

		private void onClick_Save(MyGuiControlButton sender)
		{
			foreach (MyDefinitionBase allDefinition in MyDefinitionManager.Static.GetAllDefinitions())
			{
				MyCubeBlockDefinition myCubeBlockDefinition = allDefinition as MyCubeBlockDefinition;
				if (myCubeBlockDefinition != null && !string.IsNullOrEmpty(myCubeBlockDefinition.Model))
				{
					MyCubeBlockDefinition.MountPoint[] array = MyCubeBuilder.AutogenerateMountpoints(MyModels.GetModel(myCubeBlockDefinition.Model), MyDefinitionManager.Static.GetCubeSize(myCubeBlockDefinition.CubeSize));
					myCubeBlockDefinition.MountPoints = Enumerable.ToArray<MyCubeBlockDefinition.MountPoint>((IEnumerable<MyCubeBlockDefinition.MountPoint>)array);
				}
			}
			MyDefinitionManager.Static.Save("CubeBlocks_*.*");
		}

		private void onClick_Reload(MyGuiControlButton sender)
		{
			MyDefinitionManager.Static.UnloadData(clearPreloaded: true);
			List<MyObjectBuilder_Checkpoint.ModItem> mods = new List<MyObjectBuilder_Checkpoint.ModItem>();
			MyDefinitionManager.Static.LoadData(mods);
		}

		private void onClick_SymmetryReset(MyGuiControlButton sender)
		{
			m_xMirroringCombo.SelectItemByIndex(-1);
			m_yMirroringCombo.SelectItemByIndex(-1);
			m_zMirroringCombo.SelectItemByIndex(-1);
			DebugXMirroringAxis = null;
			DebugYMirroringAxis = null;
			DebugZMirroringAxis = null;
		}

		public override bool Update(bool hasFocus)
		{
			bool result = base.Update(hasFocus);
			m_currentMirrorAxisLabel.Text = "Mirror axis: " + MyCubeBuilderGizmo.CurrentBlockMirrorAxis;
			m_currentAxisLabel.Text = "Block axis: " + MyCubeBuilderGizmo.CurrentBlockMirrorOption;
			return result;
		}
	}
}
